<?php
/**
 * @Author:      zk
 * @DateTime:    18-7-12 下午3:20
 * @Description: hrm 库 中 用户相关
 */
class HrmUser extends Eloquent
{
    protected $connection = 'mysql_hrm';
    
    protected $table = 'hr_user';
    
    /**
     * 用户性别数组
     * @var array 
     */
    public static $sexArr = [
        1 => '男',
        2 => '女',
    ];
    
    /**
     * @Author:      zk
     * @DateTime:    18-7-12 下午4:10
     * @Description: 根据hrm平台的用户id获取用户信息
     * @param int $user_id  hrm平台的用户id
     * @return array
     */
    public static function getUserInfoById($user_id){
        $selectArr = [
            'hr_user.real_name AS hr_user_name', 'hr_user.sex AS hr_sex', 'hr_user.email AS hr_email', 'hr_user.telephone AS hr_telephone',
            'hr_position.name AS hr_position_name', 'hr_position.department_id', 
            'hr_department.name AS hr_department_name', 
        ];
        $info = HrmUser::leftJoin('hr_position', 'hr_user.position_id', '=', 'hr_position.position_id')
                ->leftJoin('hr_department', 'hr_position.department_id', '=', 'hr_department.department_id')
                ->select($selectArr)
                ->where('hr_user.user_id', $user_id)
                ->first();
        return $info;
    }

    /**
     * @Author:      yyc
     * @DateTime:    18-8-2 下午2:10
     * @Description: 根据hrm平台的用户id批量获取用户信息
     * @param array $userIdArray  hrm平台的用户id
     * @return array
     */
    public static function getUserInfoByIdArray($userIdArray)
    {
        $selectArr = [
            'hr_department.department_id as approvalHrmId', 'hr_department.name AS approvalHrmName',
            'hr_user.user_id as approvalHrmUserId',
        ];
        $info = HrmUser::leftJoin('hr_position', 'hr_user.position_id', '=', 'hr_position.position_id')
            ->leftJoin('hr_department', 'hr_position.department_id', '=', 'hr_department.department_id')
            ->select($selectArr)
            ->whereIn('hr_user.user_id', $userIdArray)
            ->get()
            ->toArray();
        return $info;
    }
    
    /**
     * @Author:      zk
     * @DateTime:    18-7-18 下午5:45
     * @Description: 根据hrm平台的部门id获取该部门下的所有用户信息
     * @param int $department_id  hrm平台的部门id
     * @param array $user_id_arr  hrm平台的用户id数组
     * @return array
     */
    public static function getUserInfoByDepartmentId($department_id, $user_id_arr = []) {
        $selectArr = [
            'hr_user.user_id AS hr_user_id', 'hr_user.real_name AS hr_user_name', 'hr_user.sex AS hr_sex', 
            'hr_user.email AS hr_email', 'hr_user.telephone AS hr_telephone',
            'hr_position.name AS hr_position_name', 'hr_position.department_id', 
            'hr_department.name AS hr_department_name', 
        ];
        $info = HrmUser::leftJoin('hr_position', 'hr_user.position_id', '=', 'hr_position.position_id')
                ->leftJoin('hr_department', 'hr_position.department_id', '=', 'hr_department.department_id')
                ->select($selectArr)
                ->where('hr_user.status', 1)
                ->where('hr_department.department_id', $department_id);
        if(!empty($user_id_arr)){
            $info->whereIn('hr_user.user_id', $user_id_arr);
        }
        $info = $info->get()->toArray();
        
        return $info;
    }
    
    /**
     * @Author:      zk
     * @DateTime:    18-7-19 下午3:45
     * @Description: 根据指定搜索条件获取hrm平台的用户信息 [支持模糊搜索]
     * @param string $condition         条件名称
     * @param int $condition_type       条件类型 [1:用户名;2:邮箱;3:手机号;4:全部;]
     * @param array $user_id_arr        hrm平台的用户id数组
     * @return array
     */
    public static function getUserInfoByCondition($condition, $condition_type, $user_id_arr = []) {
        $selectArr = [
            'hr_user.user_id AS hr_user_id', 'hr_user.real_name AS hr_user_name', 'hr_user.sex AS hr_sex', 
            'hr_user.email AS hr_email', 'hr_user.telephone AS hr_telephone',
            'hr_position.name AS hr_position_name', 'hr_position.department_id', 
            'hr_department.name AS hr_department_name', 
        ];
        $info = HrmUser::leftJoin('hr_position', 'hr_user.position_id', '=', 'hr_position.position_id')
                ->leftJoin('hr_department', 'hr_position.department_id', '=', 'hr_department.department_id')
                ->select($selectArr)
                ->where('hr_user.status', 1)
                ->orderBy('hr_position.department_id')
                ->orderBy('hr_user.user_id');
        if(!empty($user_id_arr)){
            $info->whereIn('hr_user.user_id', $user_id_arr);
        }
        switch ($condition_type) {
            case 1:
                $info->where('hr_user.real_name', 'like', '%'.$condition.'%');
                break;
            case 2:
                $info->where('hr_user.email', 'like', '%'.$condition.'%');
                break;
            case 3:
                $info->where('hr_user.telephone', 'like', '%'.$condition.'%');
                break;
            case 4:
                $info->orWhere('hr_user.real_name', 'like', '%'.$condition.'%')
                    ->orWhere('hr_user.email', 'like', '%'.$condition.'%')
                    ->orWhere('hr_user.telephone', 'like', '%'.$condition.'%');
                break;
            default:
                break;
        }
        
        $info = $info->get()->toArray();
        
        return $info;
    }
    
    /**
     * @Author:      zk
     * @DateTime:    18-7-27 上午11:21
     * @Description: 根据hrm平台用户id获取hrm平台的用户信息
     * @param array $hrm_user_id_arr hrm平台用户id数组
     * @return array 以hrm_user_id为下标的二维数组
     */
    public static function getHrmUserInfoByHrmUserid($hrm_user_id_arr){
        $result = [];
        if(!is_array($hrm_user_id_arr)){
            return $result;
        }
        $selectArr = [
            'hr_user.user_id AS hr_user_id', 'hr_user.real_name AS hr_user_name', 'hr_user.sex AS hr_sex', 
            'hr_user.email AS hr_email', 'hr_user.telephone AS hr_telephone',
            'hr_position.name AS hr_position_name', 'hr_position.department_id', 
            'hr_department.name AS hr_department_name', 
        ];
        $list = HrmUser::leftJoin('hr_position', 'hr_user.position_id', '=', 'hr_position.position_id')
                ->leftJoin('hr_department', 'hr_position.department_id', '=', 'hr_department.department_id')
                ->select($selectArr)
                ->where('hr_user.status', 1)
                ->whereIn('hr_user.user_id', $hrm_user_id_arr)
                ->get()
                ->toArray();
        
        if(!empty($list)){
            foreach ($list as $v) {
                $result[$v['hr_user_id']] = $v;
            }
        }
        return $result;
    }
    
}
